const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo:null,
        userbg:null,
        userbgID:null,

    },
    getMore(){
        wx.getUserProfile({
          desc: '授权',
          success:res=>{
            // wx.showToast({
            //     title: '授权成功!',
            //   })
              this.getHeadBg()
            
              console.log(res);
              app.globalData.userInfo=res;
              this.setData({
                  userinfo:res
              })
              wx.cloud.callFunction({
                name:"loginsave",

              }).then(result=>{
                let openid = result.result.openid;
                app.globalData.userInfo.openid = openid
                let userCollect = wx.cloud.database().collection("login_user");userCollect.where({
                  "_openid":openid
                }).get().then(user=>{
                  if(user.data.length>0){
                    wx.showToast({
                      title: '欢迎回归！',
                    })
                   
                  }
                  else{
                    userCollect.add({
                     data:{
                      userinfo:res.userInfo
                     }
                    })
                    wx.showToast({
                      title: '欢迎使用！',
                    })
                  }
                })
              })
          },
          fail:err=>{
            wx.showToast({
                icon:"error",
                title: '授权失败!',
              })
              console.log(err);
          }
        })
    },
    updatabg(){
        wx.chooseImage({
          count: 1,
          success:(e)=>{
              console.log(e);
              this.setData({
                userbg:e.tempFilePaths[0]
              })
              let headBgName = new Date().getTime()+""+Math.random()
              let extname = e.tempFilePaths[0].substr(e.tempFilePaths[0].indexOf("."))
              wx.cloud.uploadFile({
                cloudPath:"heading/userbg"+headBgName+extname,
                filePath:e.tempFilePaths[0],
              }).then(re=>{  
                this.setData({
                  userbgID:re.fileID
                })
                wx.showToast({
                  title: '封面上传成功！',
                })
                this.setHeadBg(re.fileID)
              }
              ).catch(err=>{
                console.log(err);
                wx.showToast({
                  icon:"error",
                  title: '封面上传失败！',
                })
              })
          }
        })
    },
    setHeadBg(path){
          let headbg = wx.cloud.database().collection('head_bg')
          headbg.get().then(result=>{
            if(result.data.length>0){
              headbg.doc(result.data[0]._id).update(
             {
              data:{
                headpath:path,            
              }
             }
              )
            }
            else{

              headbg.add({
               data:{
                headpath:path,
                nickname:this.data.userinfo.userInfo.nickName
               }
              })
            }
          }).then(res=>{
            wx.showToast({
              title: '上传成功',
            })
          }).catch(error=>{
            wx.showToast({
              icon:"error",
              title: '上传失败',
            })
          })
    },  
    getHeadBg(){
      wx.cloud.database().collection("head_bg").get().then(res=>{
        console.log(res);
        this.setData({
          userbg:res.data[0].headpath
        })
      })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({
        userinfo: app.globalData.userInfo
      })
      if(this.data.userinfo){
        if(!this.data.userbg){
          this.getHeadBg()
        }
      }

    },


})